#include "Face.h"

#include "ImageUtility.h"

#include <QDebug>
#include <QColor>
#include <QVector2D>
Face::Face()
{

}

Face::Face(QRect face, QRect leftEye, QRect rightEye, QRect nose, QRect mouth, QImage image) :
    rectFace(face),
    rectLeftEye(leftEye),
    rectRightEye(rightEye),
    rectNose(nose),
    rectMouth(mouth),
    imageFace(image)
{

}

Face::Face(const Face &face) :
    rectFace(face.rectFace),
    rectLeftEye(face.rectLeftEye),
    rectRightEye(face.rectRightEye),
    rectNose(face.rectNose),
    rectMouth(face.rectMouth),
    imageFace(face.imageFace)
{

}

QRect Face::adjustWithinFace(QRect rect)
{
    QRect newRect = rect;
    QSize size = newRect.size();
    newRect.setTopLeft(QPoint(newRect.x() - rectFace.x(), newRect.y() - rectFace.y()));
    newRect.setSize(size);
    return newRect;
}

bool Face::isSquinting()
{
    if (rectLeftEye.isNull())
        return false;

    static QVector<float> values;

    QRect leftEyeRect = this->adjustWithinFace(rectLeftEye);
    QImage leftEye = imageFace.copy(leftEyeRect);

    leftEye = ImageUtility::smooth(leftEye, 3, ImageUtility::GAUSSIAN);


    QVector2D center(leftEye.width()/2, leftEye.height()/2);
    float total = 0.0f;
    for (int r = 0; r < leftEye.height(); r++) {
        for (int c = 0; c < leftEye.width(); c++) {
            QColor pixel = leftEye.pixel(c, r);
            QVector2D point(c, r);
            float distance = QVector2D(point - center).length();
            if (distance > 0)
                total += pixel.value()/distance;
        }
    }

    total /= (leftEye.width() * leftEye.height());

    return false;
}

QImage Face::image()
{
    return imageFace;
}

QRect Face::rect()
{
    return rectFace;
}

QVector<QRect> Face::eyesRects()
{
   QVector<QRect> eyes;
   eyes.append(rectLeftEye);
   eyes.append(rectRightEye);
   return eyes;
}

QRect Face::leftEyeRect()
{
    return rectLeftEye;
}

QRect Face::rightEyeRect()
{
    return rectRightEye;
}

QRect Face::noseRect()
{
    return rectNose;
}

QRect Face::mouthRect()
{
    return rectMouth;
}

void Face::setFaceImage(QImage face)
{
    imageFace = face;
}

void Face::setFaceRect(QRect face)
{
    rectFace = face;
}

void Face::setEyesRects(QRect leftEye, QRect rightEye)
{
    rectLeftEye = leftEye;
    rectRightEye = rightEye;
}

void Face::setNoseRect(QRect nose)
{
    rectNose = nose;
}

void Face::setMouthRect(QRect mouth)
{
    rectMouth = mouth;
}
